import {
  Body,
  Controller,
  Get,
  Post,
  Query,
  Res,
  UseGuards,
} from '@nestjs/common';
import { ResumeService } from './resume_list.service';
import { JwtAuthGuard } from '@/authGuard/jwtAuthGuard';
import {
  ApplyTemplateDto,
  EditSectionNameDto,
  EntryDto,
  PersonalDetailDto,
  SortEntryDto,
  ToggleHideEntryDto,
  UpdateCustomizationDto,
  addContentParamsDto,
  addCustomizationDto,
  addResumeParamsDto,
  delContentParamsDto,
  editContentParamsDto,
  getResumeParamsDto,
} from './dto';
import { CommonGuard } from '@/authGuard/common.guard';
import { ThrottlerProxyGuard } from '@/authGuard/throttler.guard';

@Controller()
export class ResumeController {
  constructor(private readonly resumeService: ResumeService) {}
  @UseGuards(JwtAuthGuard)
  @UseGuards(CommonGuard)
  @Get('getResumeDetail')
  getResumeDetail(@Query() params: getResumeParamsDto) {
    return this.resumeService.getResumeDetail(params);
  }
  @UseGuards(JwtAuthGuard)
  @Get('getResumes')
  getResumes() {
    return this.resumeService.getResumes();
  }

  @UseGuards(JwtAuthGuard)
  @Get('validResume')
  validResume() {
    return this.resumeService.validResume();
  }

  @Get('getResumeDetailWithoutGuards')
  getResumeDetailWithoutGuards(@Query() params: getResumeParamsDto) {
    return this.resumeService.getResumeDetailWithoutGuards(params);
  }

  @UseGuards(JwtAuthGuard)
  @UseGuards(CommonGuard)
  @Post('addResumeDetail')
  addResumeDetail(@Body() addResumeParams: addResumeParamsDto) {
    return this.resumeService.addResumeDetail(addResumeParams);
  }
  @UseGuards(JwtAuthGuard)
  @UseGuards(CommonGuard)
  @Post('addContent')
  addContent(@Body() addContentParams: addContentParamsDto) {
    return this.resumeService.addContent(addContentParams);
  }

  @UseGuards(JwtAuthGuard)
  @UseGuards(CommonGuard)
  @Post('editContent')
  editContent(@Body() editContentParams: editContentParamsDto) {
    return this.resumeService.editContent(editContentParams);
  }

  @UseGuards(JwtAuthGuard)
  @UseGuards(CommonGuard)
  @Post('delContent')
  delContent(@Body() delContentParams: delContentParamsDto) {
    return this.resumeService.delContent(delContentParams);
  }

  @UseGuards(JwtAuthGuard)
  @UseGuards(CommonGuard)
  @Post('addEntry')
  addEntry(@Body() addEntryParams: EntryDto) {
    return this.resumeService.addEntry(addEntryParams);
  }

  @UseGuards(JwtAuthGuard)
  @UseGuards(CommonGuard)
  @Post('delEntry')
  delEntry(@Body() addEntryParams: EntryDto) {
    return this.resumeService.delEntry(addEntryParams);
  }

  @UseGuards(JwtAuthGuard)
  @UseGuards(CommonGuard)
  @Get('deleteResume')
  deleteResume(@Query() addEntryParams: EntryDto) {
    return this.resumeService.deleteResume(addEntryParams);
  }

  @UseGuards(JwtAuthGuard)
  @UseGuards(CommonGuard)
  @Post('editEntry')
  editEntry(@Body() params: EntryDto) {
    return this.resumeService.editEntry(params);
  }

  @UseGuards(JwtAuthGuard)
  @UseGuards(CommonGuard)
  @Post('editSectionName')
  editSectionName(@Body() params: EditSectionNameDto) {
    return this.resumeService.editSectionName(params);
  }

  @UseGuards(JwtAuthGuard)
  @UseGuards(CommonGuard)
  @Get('toggleHideEntry')
  toggleHideEntry(@Query() params: ToggleHideEntryDto) {
    return this.resumeService.toggleHideEntry(params);
  }

  @UseGuards(JwtAuthGuard)
  @UseGuards(CommonGuard)
  @Post('sortEntry')
  sortEntry(@Body() params: SortEntryDto) {
    return this.resumeService.sortEntry(params);
  }

  @UseGuards(JwtAuthGuard)
  @UseGuards(CommonGuard)
  @Post('updatePersonalDetail')
  updatePersonalDetail(@Body() params: PersonalDetailDto) {
    return this.resumeService.updatePersonalDetail(params);
  }

  @UseGuards(JwtAuthGuard)
  @Post('applyTemplate')
  applyTemplate(@Body() params: ApplyTemplateDto) {
    return this.resumeService.applyTemplate(params);
  }

  @UseGuards(JwtAuthGuard)
  @UseGuards(CommonGuard)
  @Post('updateCustomization')
  updateCustomization(@Body() params: UpdateCustomizationDto) {
    return this.resumeService.updateCustomization(params);
  }

  @UseGuards(JwtAuthGuard)
  @UseGuards(CommonGuard)
  @Post('addCustomization')
  addCustomization(@Body() params: addCustomizationDto) {
    return this.resumeService.addCustomization(params);
  }

  @UseGuards(ThrottlerProxyGuard)
  @UseGuards(JwtAuthGuard)
  @UseGuards(CommonGuard)
  @Post('downloadFile')
  async downloadFile(@Body() params: any, @Res() res) {
    res.setHeader('Access-Control-Expose-Headers', `Content-Disposition`);
    res.setHeader(
      'Content-Disposition',
      `attachment; filename="${encodeURIComponent(
        params?.fileName || '百捷简历',
      )}.pdf"`,
    );

    res.setHeader('Content-Type', 'application/octet-stream,charset=UTF-8');
    const result = await this.resumeService.downloadFile(params);
    console.log(result);

    res.end(result);
  }

  @UseGuards(ThrottlerProxyGuard)
  @UseGuards(JwtAuthGuard)
  @UseGuards(CommonGuard)
  @Post('downloadFileById')
  async downloadFileById(@Body() params: any, @Res() res) {
    res.setHeader('Access-Control-Expose-Headers', `Content-Disposition`);
    res.setHeader(
      'Content-Disposition',
      `attachment; filename="${encodeURIComponent(
        params?.fileName || '百捷简历',
      )}.pdf"`,
    );

    res.setHeader('Content-Type', 'application/octet-stream,charset=UTF-8');
    const result = await this.resumeService.downloadFileById(params);
    res.end(result);
  }
}
